<?php
namespace Pw\MainBundle\Entity;

use Pw\MainBundle\Entity\Talent;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
class Hero
{
	const SIDE_ADORN = 'a';
	const SIDE_DOKT = 'd';
	private $id;
	private $roleId;
	private $mirrorId;
	private $side;
	private $name;
	private $lore;
	private $dateCreated;
	//stats
	private $hp;
	private $mp;
	private $speed;
	private $strength;									//Сила
	private $intelligence;								//Разум
	private $agility;									//Проворство
	private $artifice;									//Хитрость
	private $tenacity;									//Стойкость
	private $will;										//Воля
	/////////////////////////////////
	private $visible;
	private $iconName;
	private $artName;

	private $icon;
	private $art;
	private $filesToRemove;

	/**
	* @var Doctrine\Common\Collections\ArrayCollection
	*/
    private $talents;
	
    private $role;
    private $mirror;

	public function __construct() {
		$this->talents = new ArrayCollection();
		$this->visible = true;
	}

	public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
		$metadata->addConstraint(new UniqueEntity(array('fields' => 'name', 'groups' => array('add'))));
		$metadata->addPropertyConstraint('icon', new Assert\Image(array(
			'maxSize' => '1024k',
			'mimeTypesMessage' => 'Please upload a valid image (1MB max)',
        )));
		$metadata->addPropertyConstraint('art', new Assert\Image(array(
			'maxSize' => '1024k',
			'mimeTypesMessage' => 'Please upload a valid image (1MB max)',
        )));
		//Name
		$metadata->addPropertyConstraint('name', new Assert\NotBlank());
        $metadata->addPropertyConstraint('name', new Assert\Length(array("max" => 100)));
		//hp
		$metadata->addPropertyConstraint('hp', new Assert\NotBlank());
		$metadata->addPropertyConstraint('hp', new Assert\Type(array('type' => 'integer')));
		$metadata->addPropertyConstraint('hp', new Assert\Min(array('limit'   => '1')));
		//mp
		$metadata->addPropertyConstraint('mp', new Assert\NotBlank());
		$metadata->addPropertyConstraint('mp', new Assert\Type(array('type' => 'integer')));
		$metadata->addPropertyConstraint('mp', new Assert\Min(array('limit'   => '1')));
		//speed
		$metadata->addPropertyConstraint('speed', new Assert\NotBlank());
		$metadata->addPropertyConstraint('speed', new Assert\Type(array('type' => 'integer')));
		$metadata->addPropertyConstraint('speed', new Assert\Min(array('limit'   => '1')));
		//strength
		$metadata->addPropertyConstraint('strength', new Assert\NotBlank());
		$metadata->addPropertyConstraint('strength', new Assert\Type(array('type' => 'integer')));
		$metadata->addPropertyConstraint('strength', new Assert\Min(array('limit'   => '1')));
		//intelligence
		$metadata->addPropertyConstraint('intelligence', new Assert\NotBlank());
		$metadata->addPropertyConstraint('intelligence', new Assert\Type(array('type' => 'integer')));
		$metadata->addPropertyConstraint('intelligence', new Assert\Min(array('limit'   => '1')));
		//agility
		$metadata->addPropertyConstraint('agility', new Assert\NotBlank());
		$metadata->addPropertyConstraint('agility', new Assert\Type(array('type' => 'integer')));
		$metadata->addPropertyConstraint('agility', new Assert\Min(array('limit'   => '1')));
		//artifice
		$metadata->addPropertyConstraint('artifice', new Assert\NotBlank());
		$metadata->addPropertyConstraint('artifice', new Assert\Type(array('type' => 'integer')));
		$metadata->addPropertyConstraint('artifice', new Assert\Min(array('limit'   => '1')));
		//tenacity
		$metadata->addPropertyConstraint('tenacity', new Assert\NotBlank());
		$metadata->addPropertyConstraint('tenacity', new Assert\Type(array('type' => 'integer')));
		$metadata->addPropertyConstraint('tenacity', new Assert\Min(array('limit'   => '1')));
		//will
		$metadata->addPropertyConstraint('will', new Assert\NotBlank());
		$metadata->addPropertyConstraint('will', new Assert\Type(array('type' => 'integer')));
		$metadata->addPropertyConstraint('will', new Assert\Min(array('limit'   => '1')));		
	}

	public function getArray4JSON(){
		$res = array();
		$res['name'] = $this->name;
		$res['mirrorId'] = $this->mirrorId;
		//$res['lore'] = $this->lore;
		$res['hp'] = $this->hp;
		$res['mp'] = $this->mp;
		$res['speed'] = $this->speed;
		$res['strength'] = $this->strength;
		$res['intelligence'] = $this->intelligence;
		$res['agility'] = $this->agility;
		$res['artifice'] = $this->artifice;
		$res['tenacity'] = $this->tenacity;
		$res['will'] = $this->will;
		$res['iconName'] = $this->iconName;
		$res['artName'] = $this->artName;
		$res['side'] = $this->side;
		return $res;
	}

	public function getAbsolutePath($fileName){
        return $this->getUploadRootDir() . '/' . $fileName;
    }

    public function getWebPath($fileName){
        return $this->getUploadDir() . '/' . $fileName;
    }
	public function getDefaultImageSrc(){
		return $this->getWebPath('default.jpg');
	}

	public function getIconSrc(){
		if (!is_file($this->getAbsolutePath($this->iconName))) {
			return $this->getDefaultImageSrc();
		}
		return $this->getWebPath($this->iconName);
	}
	public function getArtSrc(){
		if (!is_file($this->getAbsolutePath($this->artName))) {
			return $this->getDefaultImageSrc();
		}
		return $this->getWebPath($this->artName);
	}

    protected function getUploadRootDir(){
        return __DIR__ . '/../../../../web/' . $this->getUploadDir();
    }

    protected function getUploadDir(){
        return 'images/heroes';
    }
	//////////////////////////////////////
	public function doUpload(){
		if ($this->icon) {
			//try to delete existing file
			if ($this->iconName) {
				$this->doDelete($this->iconName);
			}
			$this->iconName = uniqid() . '.' . $this->icon->guessExtension();
			$this->icon->move($this->getUploadRootDir(), $this->iconName);
			unset($this->icon);			
		}
		if ($this->art) {
			if ($this->artName) {
				$this->doDelete($this->artName);
			}
			$this->artName = uniqid() . '.' . $this->art->guessExtension();
			$this->art->move($this->getUploadRootDir(), $this->artName);
			unset($this->art);			
		}
	}
	private function doDelete($fName){
		$file = $this->getAbsolutePath($fName);
		if (is_file($file)) {
			unlink($file);
		}
	}
	public function preRemove(){
		$this->filesToRemove[] = $this->iconName;
		$this->filesToRemove[] = $this->artName;
	}
	public function postRemove(){
		if (is_array($this->filesToRemove)) {
			foreach ($this->filesToRemove as $fileName) {
				$this->doDelete($fileName);
			}
		}
	}
	//////////////////////////////////////
	public function getIcon(){
		return $this->icon;
	}
	public function setIcon($icon){
		$this->icon = $icon;
	}
	public function getArt(){
		return $this->art;
	}
	public function setArt($art){
		$this->art = $art;
	}
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set roleId
     *
     * @param integer $roleId
     * @return Hero
     */
    public function setRoleId($roleId)
    {
        $this->roleId = $roleId;
    
        return $this;
    }

    /**
     * Get roleId
     *
     * @return integer 
     */
    public function getRoleId()
    {
        return $this->roleId;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Hero
     */
    public function setName($name)
    {
        $this->name = $name;
    
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set lore
     *
     * @param string $lore
     * @return Hero
     */
    public function setLore($lore)
    {
        $this->lore = $lore;
    
        return $this;
    }

    /**
     * Get lore
     *
     * @return string 
     */
    public function getLore()
    {
        return $this->lore;
    }

    /**
     * Set hp
     *
     * @param integer $hp
     * @return Hero
     */
    public function setHp($hp)
    {
        $this->hp = $hp;
    
        return $this;
    }

    /**
     * Get hp
     *
     * @return integer 
     */
    public function getHp()
    {
        return $this->hp;
    }

    /**
     * Set mp
     *
     * @param integer $mp
     * @return Hero
     */
    public function setMp($mp)
    {
        $this->mp = $mp;
    
        return $this;
    }

    /**
     * Get mp
     *
     * @return integer 
     */
    public function getMp()
    {
        return $this->mp;
    }

    /**
     * Set speed
     *
     * @param integer $speed
     * @return Hero
     */
    public function setSpeed($speed)
    {
        $this->speed = $speed;
    
        return $this;
    }

    /**
     * Get speed
     *
     * @return integer 
     */
    public function getSpeed()
    {
        return $this->speed;
    }

    /**
     * Set strength
     *
     * @param integer $strength
     * @return Hero
     */
    public function setStrength($strength)
    {
        $this->strength = $strength;
    
        return $this;
    }

    /**
     * Get strength
     *
     * @return integer 
     */
    public function getStrength()
    {
        return $this->strength;
    }

    /**
     * Set intelligence
     *
     * @param integer $intelligence
     * @return Hero
     */
    public function setIntelligence($intelligence)
    {
        $this->intelligence = $intelligence;
    
        return $this;
    }

    /**
     * Get intelligence
     *
     * @return integer 
     */
    public function getIntelligence()
    {
        return $this->intelligence;
    }

    /**
     * Set agility
     *
     * @param integer $agility
     * @return Hero
     */
    public function setAgility($agility)
    {
        $this->agility = $agility;
    
        return $this;
    }

    /**
     * Get agility
     *
     * @return integer 
     */
    public function getAgility()
    {
        return $this->agility;
    }

    /**
     * Set artifice
     *
     * @param integer $artifice
     * @return Hero
     */
    public function setArtifice($artifice)
    {
        $this->artifice = $artifice;
    
        return $this;
    }

    /**
     * Get artifice
     *
     * @return integer 
     */
    public function getArtifice()
    {
        return $this->artifice;
    }

    /**
     * Set tenacity
     *
     * @param integer $tenacity
     * @return Hero
     */
    public function setTenacity($tenacity)
    {
        $this->tenacity = $tenacity;
    
        return $this;
    }

    /**
     * Get tenacity
     *
     * @return integer 
     */
    public function getTenacity()
    {
        return $this->tenacity;
    }

    /**
     * Set will
     *
     * @param integer $will
     * @return Hero
     */
    public function setWill($will)
    {
        $this->will = $will;
    
        return $this;
    }

    /**
     * Get will
     *
     * @return integer 
     */
    public function getWill()
    {
        return $this->will;
    }

    /**
     * Set dateCreated
     *
     * @param \DateTime $dateCreated
     * @return Hero
     */
    public function setDateCreated($dateCreated)
    {
        $this->dateCreated = $dateCreated;
    
        return $this;
    }

    /**
     * Get dateCreated
     *
     * @return \DateTime 
     */
    public function getDateCreated()
    {
        return $this->dateCreated;
    }
	
	public function getIconName(){
		return $this->iconName;
	}
    /**
     * Set iconName
     *
     * @param string $iconName
     * @return Hero
     */
    public function setIconName($iconName)
    {
		$this->iconName = $iconName;
        return $this;
    }
	
	public function getArtName(){
		return $this->artName;
	}
    /**
     * Set artName
     *
     * @param string $artName
     * @return Hero
     */
    public function setArtName($artName)
    {
        $this->artName = $artName;
        return $this;
    }
    /**
     * Add talents
     *
     * @param \Pw\MainBundle\Entity\Talent $talents
     * @return Hero
     */
    public function addTalent(Talent $talents)
    {
        $this->talents[] = $talents;
    
        return $this;
    }

    /**
     * Remove talents
     *
     * @param \Pw\MainBundle\Entity\Talent $talents
     */
    public function removeTalent(Talent $talents)
    {
        $this->talents->removeElement($talents);
    }

    /**
     * Get talents
     *
     * @return Doctrine\Common\Collections\ArrayCollection 
     */
    public function getTalents()
    {
        return $this->talents;
    }

    /**
     * Set visible
     *
     * @param boolean $visible
     * @return Hero
     */
    public function setVisible($visible)
    {
        $this->visible = $visible;
    
        return $this;
    }

    /**
     * Get visible
     *
     * @return boolean 
     */
    public function getVisible()
    {
        return $this->visible;
    }

    /**
     * Set side
     *
     * @param string $side
     * @return Hero
     */
    public function setSide($side)
    {
		if ($side === self::SIDE_ADORN || $side === self::SIDE_DOKT) {
			$this->side = $side;
		}    
        return $this;
    }

    /**
     * Get side
     *
     * @return string 
     */
    public function getSide()
    {
        return $this->side;
    }

    /**
     * Set role
     *
     * @param \Pw\MainBundle\Entity\Role $role
     * @return Hero
     */
    public function setRole(\Pw\MainBundle\Entity\Role $role = null)
    {
        $this->role = $role;
    
        return $this;
    }

    /**
     * Get role
     *
     * @return \Pw\MainBundle\Entity\Role 
     */
    public function getRole()
    {
        return $this->role;
    }

    /**
     * Set mirrorId
     *
     * @param integer $mirrorId
     * @return Hero
     */
    public function setMirrorId($mirrorId)
    {
        $this->mirrorId = $mirrorId;
    
        return $this;
    }

    /**
     * Get mirrorId
     *
     * @return integer 
     */
    public function getMirrorId()
    {
        return $this->mirrorId;
    }

    /**
     * Set mirror
     *
     * @param \Pw\MainBundle\Entity\Hero $mirror
     * @return Hero
     */
    public function setMirror(\Pw\MainBundle\Entity\Hero $mirror = null)
    {
        $this->mirror = $mirror;
    
        return $this;
    }

    /**
     * Get mirror
     *
     * @return \Pw\MainBundle\Entity\Hero 
     */
    public function getMirror()
    {
        return $this->mirror;
    }
}